/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.dusa.worklog.viewer;

import cz.dusa.worklog.datalib.WorkDay;
import java.sql.Date;
import java.sql.Time;
import java.util.List;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
import org.openide.util.NbBundle;

/**
 *
 * @author vdusa
 */
public class WorkDaysTableModel extends AbstractTableModel {

    private List<WorkDay> workDays;

    private enum COLUMNS {

        DAY_COL,
        DESCRIPTION_COL,
        START_COL,
        END_COL,
        MINUSTIME_COL
    }

    WorkDaysTableModel(List<WorkDay> workDays, TableModelListener listener) {
        addTableModelListener(listener);
        setWorkDays(workDays);
    }

    public int getRowCount() {
        return getWorkDays().size();
    }

    public int getColumnCount() {
        return COLUMNS.values().length;
    }

    public Object getValueAt(int row, int column) {
        WorkDay wd = workDays.get(row);

        switch (getColumn(column)) {
            case DAY_COL:
                return wd.getWorkDate(); //workDate
            case DESCRIPTION_COL:
                return wd.getDescription(); //description
            case START_COL:
                return wd.getStartOfWork(); //startOfWork
            case END_COL:
                return wd.getEndOfWork(); //endOfWork
            case MINUSTIME_COL:
                return wd.getMinusTime(); //minusTime
            default:
                throw new RuntimeException("Unknown column");
        }
    }

    @Override
    public Class<?> getColumnClass(int column) {
        switch (getColumn(column)) {
            case DAY_COL:
                return Date.class; //workDate
            case DESCRIPTION_COL:
                return String.class; //description
            case START_COL:
                return Time.class; //startOfWork
            case END_COL:
                return Time.class; //endOfWork
            case MINUSTIME_COL:
                return Double.class; //minusTime
            default:
                throw new RuntimeException("Unknown column");
        }
    }

    @Override
    public String getColumnName(int column) {
        switch (getColumn(column)) {
            case DAY_COL:
                return NbBundle.getMessage(ProjectEditorTopComponent.class, "DayColName");
            case DESCRIPTION_COL:
                return NbBundle.getMessage(ProjectEditorTopComponent.class, "DescriptionColName");
            case START_COL:
                return NbBundle.getMessage(ProjectEditorTopComponent.class, "StartColName");
            case END_COL:
                return NbBundle.getMessage(ProjectEditorTopComponent.class, "EndColName");
            case MINUSTIME_COL:
                return NbBundle.getMessage(ProjectEditorTopComponent.class, "MinusTimeColName");
            default:
                throw new RuntimeException("Unknown column");
        }
    }

    @Override
    public boolean isCellEditable(int row, int column) {
        return true;
    }

    @Override
    public void setValueAt(Object o, int row, int column) {
        WorkDay wd = workDays.get(row);

        switch (getColumn(column)) {
            case DAY_COL:
                wd.setWorkDate((Date) o);
                break;
            case DESCRIPTION_COL:
                wd.setDescription((String) o);
                break;
            case START_COL:
                wd.setStartOfWork((Time) o);
                break;
            case END_COL:
                wd.setEndOfWork((Time) o);
                break;
            case MINUSTIME_COL:
                wd.setMinusTime((Double) o);
                break;
            default:
                throw new RuntimeException("Unknown column");
        }

        fireTableDataChanged();
    }

    /**
     * @return the workDays
     */
    private List<WorkDay> getWorkDays() {
        return workDays;
    }

    /**
     * @param workDays the workDays to set
     */
    private void setWorkDays(List<WorkDay> workDays) {
        this.workDays = workDays;
    }

    private int getColumnIndex(COLUMNS column) {
        switch (column) {
            case DAY_COL:
                return 0;
            case DESCRIPTION_COL:
                return 1;
            case START_COL:
                return 2;
            case END_COL:
                return 3;
            case MINUSTIME_COL:
                return 4;
            default:
                throw new RuntimeException("Unknown column");
        }
    }

    private COLUMNS getColumn(int i) {
        switch (i) {
            case 0:
                return COLUMNS.DAY_COL;
            case 1:
                return COLUMNS.DESCRIPTION_COL;
            case 2:
                return COLUMNS.START_COL;
            case 3:
                return COLUMNS.END_COL;
            case 4:
                return COLUMNS.MINUSTIME_COL;
            default:
                throw new RuntimeException("Unknown column");
        }
    }
}
